package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.Product;
import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.apache.http.util.TextUtils;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 云仓推荐
 */
public class getYuncangHot extends RESTBase {
    private final static String RANGE = "10";//分页数量

    private String ip;
    private String account;
    private String password;

    public getYuncangHot(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Product> getData(String dimId1, String dimId2, String dimId3, String dimId4,
                                      String dimId5, String dimId6, String timeSpan, int page)
            throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(dimId1, dimId2, dimId3, dimId4,dimId5,dimId6,timeSpan,page), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Product> productInventories= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                Product Product = new Product();
                Product.setProductValue(jsonArray.getJSONArray(i).getString(0));
                Product.setPriceList(jsonArray.getJSONArray(i).getString(1));
                Product.setSales(jsonArray.getJSONArray(i).getString(2));
                Product.setProductId(jsonArray.getJSONArray(i).getString(3));
                Product.setProductName(jsonArray.getJSONArray(i).getString(4));
                Product.setBrandName(jsonArray.getJSONArray(i).getString(5));
               productInventories.add(Product);
            }
        }
        System.err.println(productInventories.size());
        return productInventories;
    }

    private MultiValueMap Data(String dimId1,String dimId2,String dimId3,String dimId4,
                               String dimId5, String dimId6,String timeSpan,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'M_PRODUCT_ID;VALUE'");//品名
        colums.add("'PRICELIST'");//	吊牌价
        colums.add("QTY");//	可用库存
        colums.add("M_PRODUCT_ID");//	商品
        colums.add("'M_PRODUCT_ID;NAME'");//图片款号
        colums.add("'M_DIM1_ID;ATTRIBNAME'");//商品的品牌名称

        params.put("columns", colums);
        params.put("table", "APP_RETAILRANK_DIM");//表名
        params.put("count", "true");

        params.put("range",RANGE);
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        HashMap expr3 = new HashMap();
        HashMap expr4 = new HashMap();
        HashMap expr5 = new HashMap();
        HashMap expr6 = new HashMap();
        HashMap expr7 = new HashMap();
        HashMap expr8 = new HashMap();
        HashMap expr9 = new HashMap();
        HashMap expr10 = new HashMap();
        HashMap expr11 = new HashMap();
        HashMap expr12 = new HashMap();

        if (!TextUtils.isEmpty(dimId1)) {
            System.err.println(dimId1);
            expr1.put("column","'M_DIM1_ID;ATTRIBNAME'");
            String a = "'"+dimId1+"'";
            expr1.put("condition",a);
        } else {
            expr1.put("column","'DAY_NO'");
            String a = "'"+timeSpan+"'";
            expr1.put("condition",a);
        }

        if (TextUtils.isEmpty(dimId2)) {
            expr2.put("column","'DAY_NO'");
            String b = "'"+timeSpan+"'";
            expr2.put("condition",b);
        } else {
            expr2.put("column","'M_DIM4_ID;ATTRIBNAME'");
            String b = "'"+dimId2+"'";
            expr2.put("condition",b);
        }

        expr3.put("expr1",expr1);
        expr3.put("expr2",expr2);
        expr3.put("combine","and");


        if (!TextUtils.isEmpty(dimId3)) {
            expr4.put("column","'M_DIM5_ID;ATTRIBNAME'");
            String c = "'"+dimId3+"'";
            expr4.put("condition",c);
        } else {
            expr4.put("column","'DAY_NO'");
            String c = "'"+timeSpan+"'";
            expr4.put("condition",c);
        }
        expr5.put("expr1",expr3);
        expr5.put("expr2",expr4);
        expr5.put("combine","and");

        if (TextUtils.isEmpty(dimId4)) {
            expr6.put("column","'DAY_NO'");
            String d = "'"+timeSpan+"'";
            expr6.put("condition",d);

        } else {
            expr6.put("column","'M_DIM6_ID;ATTRIBNAME'");
            String d = "'"+dimId4+"'";
            expr6.put("condition",d);
        }
        expr7.put("column","'DAY_NO'");
        String e = "'"+timeSpan+"'";
        expr7.put("condition",e);

        expr8.put("expr1",expr6);
        expr8.put("expr2",expr7);
        expr8.put("combine","and");

        if (!TextUtils.isEmpty(dimId5)) {
            System.err.println(dimId5);
            expr9.put("column","'M_DIM2_ID;ATTRIBNAME'");
            String a = "'"+dimId5+"'";
            expr9.put("condition",a);
        } else {
            expr9.put("column","'DAY_NO'");
            String a = "'"+timeSpan+"'";
            expr9.put("condition",a);
        }

        if (TextUtils.isEmpty(dimId6)) {
            expr10.put("column","'DAY_NO'");
            String b = "'"+timeSpan+"'";
            expr10.put("condition",b);
        } else {
            expr10.put("column","'M_DIM3_ID;ATTRIBNAME'");
            String b = "'"+dimId6+"'";
            expr10.put("condition",b);
        }
        expr11.put("expr1",expr9);
        expr11.put("expr2",expr10);
        expr11.put("combine","and");

        expr12.put("expr1",expr5);
        expr12.put("expr2",expr8);
        expr12.put("combine","and");


        paramss.put("expr1",expr11);
        paramss.put("expr2",expr12);
        paramss.put("combine","and");

        HashMap orderby = new HashMap();

        orderby.put("column", "QTY");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getYuncangHot g = new getYuncangHot("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("","","","上衣","2020","秋冬","",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
